﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class ProjectUserInfoController : Controller
    {
        readonly IProjectUserInfoServices _projectUser;
        readonly IUserServices _user;
        readonly IProjectInfoServices _project;
        public ProjectUserInfoController(IProjectUserInfoServices projectUser, IUserServices user, IProjectInfoServices project)
        {
            _projectUser = projectUser;
            _user = user;
            _project = project;
        }

        [HttpPost, Route("Insert")]
        public ApiResult Insert([FromBody] Models.ProjectUserIdsDto datas)
        {
            if (datas == null || datas.ProjectId == 0 || datas.UserId.Count <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            // 判断该工程当前是否为删除状态
            if (!_project.HasExistId(datas.ProjectId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_projectUser.Insert(datas, userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Delete")]
        public ApiResult Delete([FromBody] Models.ProjectUserIdDto datas)
        {
            if (datas == null || datas.ProjectId == 0 || datas.UserId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_projectUser.HasExist(datas)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }

            if (!_projectUser.Delete(datas)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }
        
        /// <summary>
        /// 获取项目实际的人，删除的除外
        /// </summary>
        [HttpPost, Route("Get")]
        public ApiResult Get(Models.PageBo<ProjectUserInfoParams> pageBo) 
        {
            if (pageBo == null || pageBo.Param == null || pageBo.Param.ProjectId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            var result = _projectUser.Query(pageBo);
            return ApiResultStatus.Success(result);
        }

        /// <summary>
        /// 获取当前项目以为的人
        /// </summary>
        [HttpGet,Route("GetUserOutsizeProject")]
        public ApiResult GetUserOutsizeProject(int projectId) 
        {
            if (projectId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            // 判断该工程当前是否为删除状态
            if (!_project.HasExistId(projectId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            var datas = _projectUser.GetUserOutsizeProject(projectId);
            return ApiResultStatus.Success(datas);
        }

        /// <summary>
        /// 获取该项目中所有的人，包括删除的
        /// </summary>
        [HttpGet, Route("GetAll")]
        public ApiResult GetAll(int projectId)
        {
            if (projectId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            var result = _projectUser.GetAll(projectId);
            return ApiResultStatus.Success(result);
        }
    }
}
